package com.flowerplus.store.app.photo;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ProgressBar;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.flowerplus.store.R;
import com.flowerplus.store.app.photo.model.PhotoItem;
import com.flowerplus.store.common.data.utils.SnackBarUtil;
import com.flowerplus.store.common.data.utils.glide.GlideUtil;
import com.flowerplus.store.common.data.utils.ui.DialogUtil;
import com.flowerplus.store.common.data.utils.ui.LayoutInflaterUtil;
import com.flowerplus.store.common.structure.mvp.BasePresenter;
import com.flowerplus.store.common.structure.ui.activity.BaseActivity;
import com.flowerplus.store.common.structure.ui.control.HackyViewPager;
import com.github.chrisbanes.photoview.PhotoView;

import java.util.ArrayList;

public class PhotoMaxListActivity extends BaseActivity {

    private HackyViewPager mViewPager;
    private ArrayList<PhotoItem> mPhotoItems;
    //识别二维码的时候先要保存 到本地地址
    private int mPosition;
    private int mWhich;
    private ProgressDialog mProgressDialog;

    private BottomSheetDialog mDialog;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initData() {
        mProgressDialog = DialogUtil.createProgressDialog(mActivity, "保存中","",false);
        mPhotoItems = getIntent().getParcelableArrayListExtra("photos");
        if (mPhotoItems == null) {
            mPhotoItems = new ArrayList<>();
        }

    }

    @Override
    protected void initEvents() {
        mViewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return mPhotoItems == null ? 0 : mPhotoItems.size();
            }

            @Override
            public Object instantiateItem(final ViewGroup container, final int position) {
                View view = LayoutInflaterUtil.inflater(R.layout.activity_photo_max, container, false);
                PhotoView photoview = (PhotoView) view.findViewById(R.id.photo_view);
                final ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
                PhotoItem photoItem = mPhotoItems.get(position);
                loadImage(container, photoview, photoItem.getOriginUrl(), photoItem.getCompressUrl(), progressBar);
                photoview.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
        });
        int currentPosition = getIntent().getIntExtra("currentPosition", 0);
        mViewPager.setCurrentItem(currentPosition);
    }

    private void loadImage(final ViewGroup container, final PhotoView photoview, final String originUrl, final String compressUrl, final ProgressBar progressBar) {
        GlideUtil.loadThumbnailAddListener(container.getContext()
                , compressUrl, originUrl
                , photoview, new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        SnackBarUtil.showStatic(container, "图片加载失败，是否重试?", "重试", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                loadImage(container, photoview, originUrl, compressUrl, progressBar);
                            }
                        });
                        progressBar.setVisibility(View.GONE);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        progressBar.setVisibility(View.GONE);
                        return false;
                    }
                });


    }

    @Override
    protected void initViews() {
        mViewPager = (HackyViewPager) findViewById(R.id.vp_view);

    }

    @Override
    public void initInject() {
//        DaggerChatComponent.builder().activityModule(getActivityModule()).applicationComponent(getApplicationComponent()).build().inject(this);
    }

    @Override
    protected int layoutResId() {
        return R.layout.activity_photo_max_list;
    }

    protected BasePresenter getPresenter() {
        return null;
    }

}
